package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.Banner;
import cn.anxingongyou.anxin.service.BannerService;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class BannerController {

    protected static final Logger logger = LoggerFactory.getLogger(BannerController.class);

    private final BannerService bannerService;
    @Autowired
    public BannerController(BannerService bannerService) {
        this.bannerService = bannerService;
    }

    @Resource
    private RedisUtil redisUtil;

    private static final String BannerList="bannerlist";



    @ApiOperation("添加轮播图信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/banner/add",method = RequestMethod.POST)
    public Map addBanner(
            @RequestBody @ApiParam(value = "轮播信息", required = true) Banner banner
    ){
        if(redisUtil.hasKey(BannerList)){
            redisUtil.del(BannerList);
        }
        banner.setCreated(new Date());
        banner.setUpdated(new Date());
        int res = bannerService.insert(banner);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除轮播图信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/banner/del/{id}",method = RequestMethod.GET)
    public Map delBanner(
            @PathVariable @ApiParam(value = "轮播图主键", required = true) Long id
    ){
        if(redisUtil.hasKey(BannerList)){
            redisUtil.del(BannerList);
        }
        int res = bannerService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新轮播图信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/banner/update",method = RequestMethod.POST)
    public Map updateBanner(
            @RequestBody @ApiParam(value = "轮播图信息,必传主键", required = true) Banner banner
    ){
        if(redisUtil.hasKey(BannerList)){
            redisUtil.del(BannerList);
        }
        int res = bannerService.updateByPrimaryKeySelective(banner);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取轮播图信息通过admin")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/banner/findByBanner",method = RequestMethod.POST)
    public Map findOneBanner(
            @RequestBody @ApiParam(value = "轮播图信息", required = true) Banner banner
    ){
        Banner banner1 = bannerService.selectOne(banner);
        if(banner1 != null) {
            return Result.<Banner>instance(200,"获取成功",banner1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取轮播图信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/banner/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "轮播图主键", required = true) Long id
    ){
        Banner banner1 = bannerService.selectOneByPrimaryKey(id);
        if(banner1 != null) {
            return Result.<Banner>instance(200,"获取成功",banner1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取所有轮播图")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/banner/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByAdmin(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "轮播图信息", required = true)  Banner banner){
        PageInfo<Banner> pageInfo = bannerService.page(pageNum, pageSize, banner);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("Api获取所有轮播图，最多会取10条")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/banner/page",method = RequestMethod.POST)
    public Map apiSelectAddressByAdmin(
            @RequestBody @ApiParam(value = "轮播图信息", required = true)  Banner banner){
        Map<String,Object> res = new HashMap<>();
        if(redisUtil.hasKey(BannerList)){
            res =(Map<String,Object>) redisUtil.get(BannerList);
            return Result.<Map>instance(200,"查询成功",res).toJsonMap();
        } else {
            PageInfo<Banner> pageInfo = bannerService.page(1, 10, banner);
            res.put("total", pageInfo.getTotal());
            res.put("pageNum", pageInfo.getPageNum());
            res.put("pageSize", pageInfo.getPageSize());
            res.put("list",pageInfo.getList());
            redisUtil.set(BannerList,res);
            return Result.<Map>instance(200,"查询成功",res).toJsonMap();
        }

    }

}
